package com.xxx.video.main.activitys;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xxx.video.R;
import com.xxx.video.channel.fragments.ChannelFragment;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.events.OpenPermissionEvent;
import com.xxx.video.commen.utils.ClientInfoUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.dynamic.fragments.DynamicFragment;
import com.xxx.video.guide.views.AppUpdateTipDialog;
import com.xxx.video.guide.views.DownloadAppProgressDialog;
import com.xxx.video.login.activitys.LoginActivity;
import com.xxx.video.main.fragments.HomeFragment;
import com.xxx.video.main.fragments.HomeRecommendFragment;
import com.xxx.video.main.presenters.IMainView;
import com.xxx.video.main.presenters.MainPresenter;
import com.xxx.video.mine.activitys.CommonWebViewActivity;
import com.xxx.video.mine.fragments.MineFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import butterknife.BindView;

public class MainActivity extends BaseActivity<MainPresenter> implements IMainView, AppUpdateTipDialog.OnAppUpdateClickListener {
    /**
     * Home tag
     */
    private static final String HOME = "Home";
    /**
     * Channel tag
     */
    private static final String CHANNEL = "Channel";
    /**
     * Dynamic tag
     */
    private static final String DYNAMIC = "Dynamic";
    /**
     * Mine tag
     */
    private static final String MINE = "Mine";
    @BindView(R.id.fl_main_top_container)
    FrameLayout flMainTopContainer;
    @BindView(R.id.bnv_main_bottom)
    BottomNavigationView bnvMainBottom;
    /**
     * home fragment
     */
    private HomeRecommendFragment mHomeFragment;
    /**
     * channel fragment
     */
    private ChannelFragment mChannelFragment;
    /**
     * dynamic fragment
     */
    private DynamicFragment mDynamicFragment;
    /**
     * mine fragment
     */
    private MineFragment mMineFragment;

    @Override
    public int getContentLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this, this);
    }

    @Override
    public boolean isOpenEventBus() {
        return true;
    }

    @Override
    public void onAttachFragment(@NotNull Fragment fragment) {
        if (mHomeFragment == null && fragment instanceof HomeRecommendFragment) {
            mHomeFragment = (HomeRecommendFragment) fragment;
        }
        if (mChannelFragment == null && fragment instanceof ChannelFragment) {
            mChannelFragment = (ChannelFragment) fragment;
        }
        if (mDynamicFragment == null && fragment instanceof DynamicFragment) {
            mDynamicFragment = (DynamicFragment) fragment;
        }
        if (mMineFragment == null && fragment instanceof MineFragment) {
            mMineFragment = (MineFragment) fragment;
        }
    }

    @Override
    public void initData(Bundle extras) {
        showRootLayoutStatus(1);
        showHomeFragment();
        ClientInfoUtils.getInstance().updateClientInfo(this);
        getPresenter().appUsefollow(0);
        getPresenter().getAppVersion();
    }

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void showHomeFragment() {
        StatusBarUtil.getInstance().statusBarLightMode(this, true);
        /*
         * if fragment ==null else create
         */
        if (null == mHomeFragment) {
            mHomeFragment = HomeRecommendFragment.newInstance(new Bundle());
            addFragment(R.id.fl_main_top_container, mHomeFragment, HOME);
        }
        /*
         * hide all fragment
         */
        hideAllFragment();
        /*
         * show fragment
         */
        showFragment(mHomeFragment);
    }

    @Override
    public void showChannelFragment() {
        /*
         * if fragment ==null else create
         */
        if (null == mChannelFragment) {
            mChannelFragment = ChannelFragment.newInstance(new Bundle());
            addFragment(R.id.fl_main_top_container, mChannelFragment, CHANNEL);
        }
        /*
         * hide all fragment
         */
        hideAllFragment();
        /*
         * show fragment
         */
        showFragment(mChannelFragment);
    }

    @Override
    public void showDynamicFragment() {
        StatusBarUtil.getInstance().statusBarLightMode(this, true);
        /*
         * if fragment ==null else create
         */
        if (null == mDynamicFragment) {
            mDynamicFragment = DynamicFragment.newInstance(new Bundle());
            addFragment(R.id.fl_main_top_container, mDynamicFragment, DYNAMIC);
        }
        /*
         * hide all fragment
         */
        hideAllFragment();
        /*
         * show fragment
         */
        showFragment(mDynamicFragment);
    }

    @Override
    public void showMineFragment() {
        StatusBarUtil.getInstance().statusBarLightMode(this, false);
        /*
         * if fragment ==null else create
         */
        if (null == mMineFragment) {
            mMineFragment = MineFragment.newInstance(new Bundle());
            addFragment(R.id.fl_main_top_container, mMineFragment, MINE);
        }
        /*
         * hide all fragment
         */
        hideAllFragment();
        /*
         * show fragment
         */
        showFragment(mMineFragment);
    }

    @Override
    public void hideAllFragment() {
        if (null != mHomeFragment) {
            hideFragment(mHomeFragment);
        }
        if (null != mChannelFragment) {
            hideFragment(mChannelFragment);
        }
        if (null != mDynamicFragment) {
            hideFragment(mDynamicFragment);
        }
        if (null != mMineFragment) {
            hideFragment(mMineFragment);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        bnvMainBottom.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.tv_main_bottom_home:
                    showHomeFragment();
                    break;
                case R.id.tv_main_bottom_channel:
                    showChannelFragment();
                    break;
                case R.id.tv_main_bottom_dynamic:
                    if (!UserManager.getInstance().isLogin()) {
                        startLoginActivity();
                        return false;
                    } else {
                        showDynamicFragment();
                    }
                    break;
                case R.id.tv_main_bottom_miner:
                    showMineFragment();
                    break;
                default:
                    break;
            }
            return true;
        });
    }

    @Override
    public void startLoginActivity() {
        intent2Activity(LoginActivity.class);
    }

    /**
     * open permission event
     *
     * @param openPermissionEvent event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOpenPermissionEvent(OpenPermissionEvent openPermissionEvent) {
        if (openPermissionEvent.permissions == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // You can directly ask for the permission.
            requestPermissions(openPermissionEvent.permissions, 200);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 200:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission is granted. Continue the action or workflow
                    // in your app.
                } else {
                    // Explain to the user that the feature is unavailable because
                    // the features requires a permission that the user has denied.
                    // At the same time, respect the user's decision. Don't link to
                    // system settings in an effort to convince the user to change
                    // their decision.
                    ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_please_open_the_read_and_write_external_storage_permission));
                }
                return;
            default:
                break;
        }
    }
    @Override
    public void showUpdateDialog(String downloadUrl) {
        AppUpdateTipDialog appUpdateTipDialog = new AppUpdateTipDialog(this, downloadUrl);
        appUpdateTipDialog.setOnAppUpdateClickListener(this);
        if (!appUpdateTipDialog.isShowing()) {
            appUpdateTipDialog.show();
        }
    }
    @Override
    protected void onDestroy() {
        getPresenter().appUsefollow(1);
        super.onDestroy();
    }

    @Override
    public void onConfirmClick(String downloadUrl) {
        DownloadAppProgressDialog downloadAppProgressDialog = new DownloadAppProgressDialog(this, downloadUrl);
        if (!downloadAppProgressDialog.isShowing()) {
            downloadAppProgressDialog.show();
        }
    }
}